﻿using MiniFox.Caching;
using MiniFox.Components;
using MiniFox.Infrastructure;
using MiniFox.Platform.Applications.Auth;
using MiniFox.Platform.Applications.Models;
using MiniFox.Platform.Applications.Providers;

namespace MiniFox.Platform.Applications
{
    /// <summary>
    /// 桌面
    /// </summary>
    public class DesktopService : ApiServiceBase, IDesktopService
    {
        /// <summary>
        /// 用户信息键名
        /// </summary>
        const string SYS_USER = @"SYS_USER";
        /// <summary>
        /// 
        /// </summary>
        const string SYS_APP = @"SYS_APP";
        /// <summary>
        /// 
        /// </summary>
        const string SYS_APP_MENU = "SYS_APP_MENU";
        /// <summary>
        /// 
        /// </summary>
        const string SYS_APP_NAV = "SYS_APP_NAV";
        /// <summary>
        /// 
        /// </summary>
        const string SYS_SHORTCUT = "SYS_SHORTCUT";


        /// <summary>
        /// 
        /// </summary>
        /// <param name="principal"></param>
        public DesktopService(SystemPrincipal principal) : base(principal)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        [AutoWired]
        protected IDesktopProvider Provider
        {
            get;
            set;
        }
        /// <summary>
        /// 获取当前用户信息
        /// </summary>
        /// <returns></returns>
        public SystemUser CurrentSystemUser
        {
            get
            {
                return CacheService.DefaultCachingProvider.Get($"{SYS_USER}-{this.Principal.AuthorizeToken.RefreshToken}", () =>
                {
                    SystemUser systemUser = this.Provider.GetSystemUser(this.Principal.GlobalID);
                    return systemUser;
                });
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerable<SystemApp> GetSystemApps()
        {
            return this.Cache.Get($"{SYS_APP}-{this.Principal.AuthorizeToken.RefreshToken}", () =>
            {
                IEnumerable<SystemApp> apps;
                if (this.Principal.IsAdministrator)
                    apps = Provider.GetSystemApps();
                else
                    apps = Provider.GetSystemApps(this.CurrentSystemUser);
                return apps;
            });
        }

        /// <summary>
        /// 创建快捷方式
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        public bool CreateShortcut(SystemNavModule module)
        {
            return this.Provider.CreateShortcut(this.CurrentSystemUser, module);
        }

        /// <summary>
        /// 删除快捷方式
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        public bool RemoveShortcut(SystemNavModule module)
        {
            return this.Provider.RemoveShortcut(this.CurrentSystemUser, module);
        }

        /// <summary>
        /// 用户桌面快捷方式
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Shortcut> GetShortcuts()
        {
            return this.Cache.Get($"{SYS_SHORTCUT}-{this.Principal.AuthorizeToken.RefreshToken}", () =>
            {
                return this.Provider.GetUserShortcuts(this.CurrentSystemUser);
            });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="appId"></param>
        /// <returns></returns>
        public virtual IEnumerable<ITreeNode> GetMenu(string appId)
        {
            return this.Cache.Get($"{SYS_APP_MENU}-{appId}-{this.Principal.AuthorizeToken.RefreshToken}", () =>
            {
                var list = this.Principal.IsAdministrator ? this.Provider.GetMenu(appId) : this.Provider.GetMenu(appId, this.CurrentSystemUser);
                TreeResultObject<SystemNavBase> tree = new TreeResultObject<SystemNavBase>(list);
                return tree.Result;
            });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="appId"></param>
        /// <returns></returns>
        public virtual IEnumerable<ITreeNode> GetNav(string appId)
        {
            return this.Cache.Get($"{SYS_APP_NAV}-{appId}-{this.Principal.AuthorizeToken.RefreshToken}", () =>
            {
                var list = this.Principal.IsAdministrator ? this.Provider.GetNavigation(appId) : this.Provider.GetNavigation(appId, this.CurrentSystemUser);
                TreeResultObject<SystemNavBase> tree = new TreeResultObject<SystemNavBase>(list);
                return tree.Result;
            });

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public virtual IEnumerable<SystemNavModule> SearchModules(string keyword)
        {
            return this.Provider.SearchModules(this.CurrentSystemUser, keyword);
        }
    }
}
